package com.onLineCar.message.sms.utils;


import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Component;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.util.Properties;

@Component
public class MailUtils {


    // 发送简单邮件
    public void sendSimpleEmail(String mailto, String title, String content) {

        // 创建JavaMailSenderImpl对象
        JavaMailSenderImpl mailSender = new JavaMailSenderImpl();
        // 设置邮件发送主机为smtp.qq.com
        mailSender.setHost("smtp.qq.com");
        // 设置邮件发送端口号为465
        mailSender.setPort(465);
        // 设置邮件发送用户名为2693919932@qq.com
        mailSender.setUsername("2693919932@qq.com");
        // 设置邮件发送密码为slybxqcpkrnedgcc
        mailSender.setPassword("slybxqcpkrnedgcc");

        // 获取系统属性
        Properties pro = System.getProperties();
        // 设置邮件SMTP需要身份验证
        pro.put("mail.smtp.auth", "true");
        // 开启SMTP的SSL加密
        pro.put("mail.smtp.ssl.enable", "true");
        // 设置邮件的socket工厂为javax.net.ssl.SSLSocketFactory
        pro.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
        // 将设置的属性传递给mailSender
        mailSender.setJavaMailProperties(pro);


        MimeMessage helper = mailSender.createMimeMessage();

        try {
            MimeMessageHelper message = new MimeMessageHelper(helper, true);  // 创建MimeMessageHelper对象
            message.setFrom("2693919932@qq.com");  // 设置发件人
            message.setTo(mailto);  // 设置收件人
            message.setSubject(title);  // 设置邮件主题
            message.setText(content);  // 设置邮件内容
            // 发送邮件
            mailSender.send(helper);  // 发送邮件
        } catch (MessagingException e) {
            throw new RuntimeException(e);
        }


    }
}
